package com.soft.idc.common.control;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.service.IdcRoleService;
import com.soft.idc.common.util.JQPage;

public class UserMangerController extends Controller {

	public void index() {
		this.render("/user/user.html");
	}

	public void queryUser() {
		int start = this.getParaToInt("start");
		int length = this.getParaToInt("length");
		IdcUser u = this.getSessionAttr("SYS_USER");
		String userRealName = this.getPara("userRealName");
		String org_name = this.getPara("org_name");
		String roleName = this.getPara("roleName");
		String mobile = this.getPara("mobile");
		String email = this.getPara("email");
		String select = "select u.user_id, u.password, u.role_id, u.org_id, u.is_lock, u.username, u.realname, "
				+ "u.mobile, u.email, o.name as org_name, r.rolename as role_name ";
		StringBuffer sqlFrom = new StringBuffer();
		sqlFrom.append(" from idc_user u left join idc_role r on r.role_id = u.role_id  ");
		sqlFrom.append(" left join idc_org o on o.org_id = u.org_id where 1=1 ");
		if (userRealName != null && !userRealName.equals("")) {
			sqlFrom.append(" AND REALNAME LIKE '%" + userRealName + "%' ");
		}

		if (org_name != null && !org_name.equals("")) {
			sqlFrom.append(" and o.name like '%" + org_name + "%' ");
		}

		if (roleName != null && !roleName.equals("")) {
			sqlFrom.append(" and r.rolename like '%" + roleName + "%' ");
		}
		if (mobile != null && !mobile.equals("")) {
			sqlFrom.append(" and u.mobile like '%" + mobile + "%' ");
		}
		if (email != null && !email.equals("")) {
			sqlFrom.append(" and u.email like '%" + email + "%' ");
		}
		int pageNum = start / length + 1;

		if (u.getOrgId() == 1) {// 1位顶级组织，最高等级用户
			Page<Record> page = Db.paginate(pageNum, length, select, sqlFrom.toString());

			JQPage jPage = new JQPage(page);
			System.out.println(JsonKit.toJson(jPage));

			this.renderJson(jPage);
		} else {//
			sqlFrom.append(" and u.org_id = ? ");
			Page<Record> page = Db.paginate(pageNum, length, select, sqlFrom.toString(),u.getOrgId());

			JQPage jPage = new JQPage(page);
			System.out.println(JsonKit.toJson(jPage));

			this.renderJson(jPage);
		}
	}

	public void add() {
		String username = this.getPara("username");
		String password = this.getPara("password");
		String realname = this.getPara("realname");
		String mobile = this.getPara("mobile");
		String email = this.getPara("email");
		String org_id = this.getPara("org_id");
		String role_id = this.getPara("role_id");
		String is_lock = this.getPara("is_lock");

		String sql = "INSERT INTO IDC_USER (username, password, realname, mobile, email, org_id, role_id, is_lock) VALUES (? , ? , ?, ?, ?, ?, ?, ?)";
		int result = Db.update(sql,
				new Object[] { username, password, realname, mobile, email, org_id, role_id, is_lock });
		this.renderJson(result);
	}

	public void delete() {
		final String userIds = this.getPara("userIds");
		ICallback call = new ICallback() {

			@Override
			public Object call(Connection conn) throws SQLException {
				// TODO Auto-generated method stub
				conn.setAutoCommit(false);
				String[] userIdArr = userIds.split(";");
				StringBuffer sb = new StringBuffer();
				sb.append("DELETE FROM IDC_USER WHERE USER_ID IN(");
				for (int i = 0; i < userIdArr.length; i++) {
					if (i == userIdArr.length - 1) {
						sb.append(userIdArr[i] + ")");
					} else {
						sb.append(userIdArr[i] + ",");
					}
				}
				Statement sta = conn.createStatement();
				int count = sta.executeUpdate(sb.toString());
				if (count == userIdArr.length) {
					conn.commit();
					conn.close();
					return true;
				} else {
					conn.rollback();
					conn.close();
					return false;
				}
			}
		};
		Object obj = Db.execute(call);
		this.renderJson(obj);
	}

	public void edit() {
		String user_id = this.getPara("user_id");
		String username = this.getPara("username");
		String password = this.getPara("password");
		String realname = this.getPara("realname");
		String mobile = this.getPara("mobile");
		String email = this.getPara("email");
		String org_id = this.getPara("org_id");
		String role_id = this.getPara("role_id");
		String is_lock = this.getPara("is_lock");
		String sql = "UPDATE IDC_USER SET username=?, password=?, realname=?,"
				+ " mobile=?, email=?, org_id=?, role_id=?, is_lock=? WHERE user_id=?";
		int result = Db.update(sql,
				new Object[] { username, password, realname, mobile, email, org_id, role_id, is_lock, user_id });
		this.renderJson(result);
	}

	public void queryRoleList() {
		List<Record> result = IdcRoleService.queryRoleList();
		this.renderJson(result);
	}
}
